﻿function AzureOnlineBlobResizeView(context) {

    var self = this;

    var dispatcher = $({});

    var spinner = new SpinnerModel();

    var container = ko.observable('');

    var blob = ko.observable(null);

    var originalBlob = ko.observable(null);

    function init(blobModel) {
        originalBlob(blobModel);
        blob(blobModel.clone());
        container(context.blobs.container());
    }

    function validate() {
        return true;
    }

    function submit() {
        var api = context.api();
        if (validate()) {
            var data = getInfo();
            var bl = blob();
            spinner.enter();
            api.blobs.setBlobProperties({
                container: container(),
                blob: bl.name(),
                properties: data,
                success: function () {
                    spinner.leave();
                    bl.cloneTo(originalBlob());
                    dispatcher.trigger('success');
                },
                error: function () {
                    spinner.leave();
                }
            });
        }
    }

    function getInfo() {
        return {
            contentLength: blob().contentLength()
        };
    }

    self.blob = blob;
    self.init = init;
    self.submit = submit;

    self.addOnSuccess = function (handler) {
        dispatcher.bind('success', handler);
    };
}